package gov.tax.rti.base.view;

import gov.tax.rti.base.util.FunctionIdUtil;
import gov.tax.rti.core.util.SecurityContext;

import org.apache.commons.lang.StringUtils;

import com.bstek.dorado.data.listener.GenericObjectListener;
import com.bstek.dorado.view.ViewState;
import com.bstek.dorado.view.manager.ViewConfig;
import com.bstek.dorado.web.DoradoContext;

public class RtiViewConfigListener  extends GenericObjectListener<ViewConfig> {
	
	@Override
	public boolean beforeInit(ViewConfig viewConfig) throws Exception {
		return true;
	}

	@Override
	public void onInit(ViewConfig viewConfig) throws Exception {
		if(viewConfig.getState() == ViewState.rendering){
			String fnId = SecurityContext.getCurrentFnId();
			if(StringUtils.isNotEmpty(fnId)){
				DoradoContext context = DoradoContext.getCurrent();
				Object value = context.getAttribute(DoradoContext.VIEW, FunctionIdUtil.RTI_FUNCTIONID_KEY);
				if(value == null){
					context.setAttribute(DoradoContext.VIEW, FunctionIdUtil.RTI_FUNCTIONID_KEY, fnId);
				}
				/*value = context.getAttribute(DoradoContext.REQUEST, "gnmkId");
				if(value == null){
					context.setAttribute(DoradoContext.REQUEST, "gnmkId", gnmkId);
				}*/
			}
		}else{
			FunctionIdUtil.initFnId( DoradoContext.getCurrent());
		}
	}
}
